<?php



declare (strict_types=1);



namespace app\base\controller\miniapp;



use app\base\logic\MemberCouponLogic;

use app\base\model\CouponModel;

use app\base\model\member\MemberModel;

use app\base\model\miniapp\RecommendAppModel;

use app\base\model\StyleModel;

use app\BaseController;

use app\common\helper\open\OpenPlatformMiniappHelper;

use app\common\logic\TokenLogic;

use app\common\model\RegisterLogsModel;

class Main extends BaseController {









    public function getRecommendMiniapps(){

        $datas = RecommendAppModel::where([

            ['shop_id','=', $this->request->shopId],

            ['is_delete','=',0]

        ])->order(['recommend_app_id'=>'desc'])->limit($this->request->limit)->select()->toArray();

        return $this->success([

            'miniapps' => $datas

        ]);

    }



    //下放风格基本配置

    public function init() {

        $shopId = $this->request->shopId;



        $style = cache('miniapp_style' . $shopId);

        if (empty($style)) {

            $miniappStyle = StyleModel::find($shopId);

            if (null === $miniappStyle) {

                return $this->error('暂未设置小程序风格');

            }

            $style = $miniappStyle->style;

            cache('miniapp_style' . $shopId, $style, 86400);

        }

        $return['style']    = $style;

        $return['shopInfo'] = $this->request->miniapp;

        return $this->success($return);

    }



    //登录

    public function passport() {

        $code = $this->request->param('code');

        $encryptedData = $this->request->param('encryptedData');

        $iv = $this->request->param('iv');

        if(empty($code) || empty($encryptedData) || empty($iv)){

            return $this->error('登录授权解析失败#01');

        }

        $Openplatform = new OpenPlatformMiniappHelper();

        $datas = $Openplatform->setShopId($this->request->shopId)->getUserInfo([

            'code'          => $code,

            'encryptedData' => $encryptedData,

            'iv'            => $iv

        ]);

        if (false === $datas) {

            return $this->error('登录授权解析失败');

        }

        $pid = $this->request->param('pid/d');



        if(!empty($pid)){ //pid的操作

            if(null === $pmember = MemberModel::find($pid)){

                $pid = 0;

            }

            if($pmember->shop_id != $this->request->shopId){

                $pid = 0;

            }

        }

        $is_new = false;

        if (null === $member = MemberModel::where([

                ['shop_id', '=', $this->request->shopId],

                ['mobile', '=', $datas['mobile']],

                ['is_delete','=',0]

            ])->find()) {



         

            $member = new MemberModel();

            $member->shop_id = $this->request->shopId;

            $member->mobile = $datas['mobile'];

            $member->level_id = 0;

            $member->open_id = $datas['openid'];

            $member->pid = $pid;

            $member->nick_name = 'V'.$this->request->shopId.date('md').mt_rand(100000,999999); //防止有特殊字的用户

            $member->save();

            $today = date('Y-m-d', $this->request->time()); 

            //新用户券

            $new_member_coupon_data=CouponModel::where([

                ['shop_id','=',$this->request->shopId],

                ['type_id','=',2],

                ['status','=',1],

                ['is_delete','=',0],

                ['start_time','<=',$today],

                ['end_time','>=',$today],

            ])->order(['update_time'=>'desc','add_time'=>'desc'])

                ->limit(5)

                ->field('coupon_id,name,coupon_money,money_limit,valid_bg_time,valid_end_time')

                ->select()->toArray();

            if(!empty($new_member_coupon_data)){

                $member_coupon_logic= new MemberCouponLogic($this->request->shopId,$member->member_id);

                foreach ($new_member_coupon_data as $coupon){

                    $member_coupon_logic->getCoupon($coupon,2,1,0);

                }

            }

            

            if(!empty($pid)){ //邀请券

                $invitation_coupon_data=CouponModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type_id','=',4],

                    ['status','=',1],

                    ['is_delete','=',0],

                    ['start_time','<=',$today],

                    ['end_time','>=',$today],

                ])->order(['update_time'=>'desc','add_time'=>'desc'])

                    ->limit(5)

                    ->field('coupon_id,name,coupon_money,money_limit,valid_bg_time,valid_end_time')

                    ->select()->toArray();

                if(!empty($invitation_coupon_data)){

                    $coupon = $invitation_coupon_data[array_rand($invitation_coupon_data,1)] ?? [];

                    if(!empty($coupon)){

                        //被邀请送券

                        $member_coupon_logic= new MemberCouponLogic($this->request->shopId,$member->member_id);

                        $member_coupon_logic->getCoupon($coupon,2,10,0);

                        //邀请送券

                        $member_coupon_logic= new MemberCouponLogic($this->request->shopId,$pid);

                        $member_coupon_logic->getCoupon($coupon,2,9,0);

                    }

                }

            }

            

            $register_logs = new RegisterLogsModel();

            $register_logs->shop_id=$this->request->shopId;

            $register_logs->mobile=$member->mobile;

            $register_logs->save();

            $is_new = true;

        }else{ //非新用户不更新Pid 了

            if(empty($member->open_id)){

                $member->open_id=$datas['openid'];

                $member->save();

            }

        }

       



    

        $return = [];

        $return['token'] = TokenLogic::getMiniToken($member->member_id);

        return $this->success($return);

    }



 



    



}

